//
// Created by Administrator on 2021/11/2.
// May Saint Diana bless you!
//
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        // 得到每一行和每一列的最大值
        unsigned int m = grid.size(),n = grid[0].size();
        vector<int>rowMax(m,0);
        vector<int>colMax(n,0);
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                rowMax[i] = max(rowMax[i],grid[i][j]);
                colMax[j] = max(colMax[j],grid[i][j]);
            }
        }
        int ans = 0;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                ans += min(rowMax[i],colMax[j])-grid[i][j];
            }
        }
        return ans;
    }
};


int main() {
    vector<vector<int>>grid{{3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0}};
    Solution solution;
    cout << solution.maxIncreaseKeepingSkyline(grid)<<endl;
    return 0;
}
